About
-
-
Good guide, but very concise.
-
-
Guide .
-
~ ZigGuide .
-
It's a confusing and not very explanatory guide, very concise and technical.
-
-
-
It's an okay place to test some things, but many times I felt lazy to do the exercise, because the statement is looong and Dora the Explorer style, while other times it's not exactly clear what should be done, or why that was done, or what other options exist.
Style Guidelines
-
snake_case
-
variables.
-
namespace.
-
-
camelCase
-
functions.
-
-
PascalCase
-
types.
-
Examples
HTTP Requests Example
const std = @import("std");
const http = @import("zig-http/http");
pub fn main() !void {
const allocator = std.heap.page_allocator;
// Create the HTTP server
var server = try http.Server.init(allocator, "0.0.0.0", 8080, handler);
std.debug.print("Server running at http://localhost:8080\n", .{});
// Start the server
try server.listen();
}
// Function that handles HTTP requests
fn handler(req: *http.Request, res: *http.Response) !void {
// Process only GET requests
if (req.method != .Get) {
res.status = .MethodNotAllowed;
try res.send("Method not allowed");
return;
}
// Get the "name" parameter from the URL
const name = req.getQueryParam("name") orelse "world";
// Create the response message
const message = try std.fmt.allocPrint(std.heap.page_allocator, "Hello, {}!", .{name});
// Set status and response body
res.status = .Ok;
try res.send(message);
// Free the memory allocated for the message
std.heap.page_allocator.free(message);
}